package com.cloud.controller;


import com.cloud.common.result.Result;
import com.cloud.service.PaymentDefaultService;
import com.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("paymentDefault")
@Slf4j
public class PaymentDefaultController {
    /**
     * 服务对象
     */
    @Resource
    private PaymentDefaultService paymentDefaultService;

//    @Value("${server.port}")
//    private String serverPort;

    /**
     * 正常访问或者异常
     * @param id
     * @return
     */
    @GetMapping("/hystrix/ok/{id}")
    public Result paymentInfoOK(@PathVariable("id") Integer id) {
        Result result = paymentDefaultService.paymentOKOrFallback(id);
        log.info("result: " + result);
        return result;
    }

    /**
     * 超时
     * @param timeout
     * @return
     */
    @GetMapping("/hystrix/timeout/{timeout}")
    public Result paymentTimeOut(@PathVariable("timeout") Long timeout) {
        Result result = paymentDefaultService.paymentTimeOut(timeout);
        log.info("result: " + result);
        return result;
    }


    /**
     * 服务熔断
     *
     * @param id
     * @return
     */
    @GetMapping("/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String result = paymentDefaultService.paymentCircuitBreaker(id);
        log.info("****result: " + result);
        return result;
    }
}